package main

import (
	"encoding/json"
	"file-server/test/inner"
	"file-server/util"
	"fmt"
	"sync/atomic"
	"time"
)

func main() {
	fmt.Println("服务启动")

	testCustomJsonCmd()
}

var url = "http://s2-test.keytop.cn"
var parkId = "592011258"
var accessToken = "85d15350778b11e9bbaa506b4b2f6421"
var accessKey = "EED96C219E83450A"

func testCustomJsonCmd() {
	//initDev()
	initTest()
	//initPro()
	uri := "/nkcStc/channel/send"

	cmd := "customJsonCmd"

	//lotCode := "8251"
	lotCode := "791002826"

	apiParam := inner.ApiParam{
		AccessKey:   accessKey,
		AccessToken: accessToken,
		LotCode:     lotCode,
		Cmd:         cmd,
	}

	//apiParam.Set("deviceIp", lotCode)
	apiParam.Set("orgDeviceType", "64")
	apiParam.Set("nodeId", 21)
	apiParam.Set("lotCode", lotCode)
	apiParam.Set("lotCode", lotCode)

	data := make(map[string]interface{})

	data["carNo"] = "123456"
	data["cardNo"] = "54321"

	apiParam.Set("data", data)

	//var count int64 = 1
	//mutilRun(&apiParam, &uri, &count)
	for true {
		doPost(&apiParam, &uri)
		time.Sleep(time.Duration(1) * time.Second)
		break
	}
}

func doPost(apiParam *inner.ApiParam, uri *string) {
	header := apiParam.Auth()
	result, err := util.PostJSONDebug(url+*uri, apiParam.Get(), header)
	if err != nil {
		fmt.Println(err)
	}

	renderResult(result)
}

var counter int64 = 0

func renderResult(result string) {
	atomic.AddInt64(&counter, 1)

	//fmt.Println("计数器", counter, "请求结果：", result)

	var data = make(map[string]interface{})

	json.Unmarshal([]byte(result), &data)

	if data["code"] != "200" {
		fmt.Println("计数器", counter, "请求结果：", result)
	} else {
		fmt.Println("计数器", counter, "是否成功", data["code"] == "200", "请求结果：", result)
	}

}

func initTest() {
	url = "http://s2-test.keytop.cn"
	parkId = "592011258"
	//accessToken = "85d15350778b11e9bbaa506b4b2f6421"
	//accessKey = "EED96C219E83450A"
	accessToken = "2d2b262db5fea2b96cbe9dd201d763b3"
	accessKey = "B5FEA2B96CBE9DD2"
}
